//
//  PhonePwdViewController.m
//  BTCoin
//
//  Created by Robin on 14/03/2018.
//  Copyright © 2018 Robin. All rights reserved.
//

#import "PhonePwdViewController.h"
#import "UIButton+Extension.h"
#import "MQVerCodeInputView.h"
#import "RegisterTextField.h"
#import "QCheckBox.h"
#import "NSString+Custom.h"
#import "UserService.h"

@interface PhonePwdViewController ()<QCheckBoxDelegate,UITextFieldDelegate> {
    NSTimer *_timer;
    int nSecond;
}
@property (nonatomic,strong) RegisterTextField *txtPwd;
@property (nonatomic,strong) RegisterTextField *txtConfirmPwd;
@property (nonatomic,strong) QCheckBox * checkAgree;
@property (nonatomic,strong) UIButton * btnCode;
@property (nonatomic,strong) UIButton * signUpBtn;
@property (nonatomic,copy) NSString * validateCode;
@end

@implementation PhonePwdViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.headView.backgroundColor = COLOR_Bg_White;
    float itemBarWidth = 30.0;
    
    UIButton * loginBtn = [UIButton initWithFrame:Rect(kScreenWidth-itemBarWidth-10.0, StatusBarHeight+(NaviBarHeight-itemBarWidth)/2, itemBarWidth, itemBarWidth) title:NSLocalizedString(@"登录", nil)];
    [loginBtn addTarget:self action:@selector(loginAction:) forControlEvents:UIControlEventTouchUpInside];
    [loginBtn setTitleColor:color_333333];
    [self setRightBtn:loginBtn];
    
    
    [self initUIHead];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)initUIHead {
    UILabel * loginLabel = [[UILabel alloc] init];
    [loginLabel setFont:[UIFont boldSystemFontOfSize:30]];
    [loginLabel setTextColor:color_333333];
    loginLabel.text = NSLocalizedString(@"手机注册", nil);
    [loginLabel setTextAlignment:NSTextAlignmentLeft];
    [self.view addSubview:loginLabel];
    [loginLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.equalTo(self.view).offset(20.0);
        make.top.equalTo(self.headView.mas_bottom).offset(30);
    }];
    
    UILabel * codeLabel = [[UILabel alloc] init];
    [codeLabel setFont:[UIFont systemFontOfSize:17]];
    [codeLabel setTextColor:color_333333];
    codeLabel.text = NSLocalizedString(@"验证码", nil);
    [codeLabel setTextAlignment:NSTextAlignmentLeft];
    [self.view addSubview:codeLabel];
    [codeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.equalTo(loginLabel);
        make.top.equalTo(loginLabel).offset(40);
    }];
    
    float height = 130;
    if (iPhone6_Plus) {
        height = 130;
    } else if (iPhoneX) {
        
    } else if (iPhone5) {
        height = 80;
    }
    
    @WeakObj(self)
    MQVerCodeInputView *verView = [[MQVerCodeInputView alloc] initWithFrame:CGRectMake(0, CGRectGetMaxY(codeLabel.frame), ScreenWidth -50, 50)];
    verView.maxLenght = 6;//最大长度
    verView.keyBoardType = UIKeyboardTypeNumberPad;
    [verView mq_verCodeViewWithMaxLenght];
    verView.block = ^(NSString *text){
        selfWeak.validateCode = text;
        [selfWeak checkLogBtnIsEnable];
    };
    verView.center = CGPointMake(self.view.center.x, self.view.center.y-height);
    [self.view addSubview:verView];
    
    UIColor *color = UIColorFromRGB(0xB2B2B2);
    _txtPwd = [[RegisterTextField alloc] initWithFrame:CGRectZero];
    _txtPwd.delegate = self;
    [_txtPwd addTarget:self action:@selector(textFieldDidChange:) forControlEvents:UIControlEventEditingChanged];
    [_txtPwd setBorderStyle:UITextBorderStyleNone];
    [_txtPwd setReturnKeyType:UIReturnKeyDone];
    _txtPwd.clearButtonMode = UITextFieldViewModeWhileEditing;
    _txtPwd.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
    _txtPwd.leftViewMode = UITextFieldViewModeAlways;
    [_txtPwd setBackgroundColor:[UIColor clearColor]];
    [_txtPwd setTextColor:UIColorFromRGB(0x343434)];
    _txtPwd.isShowTextBool = YES;
    _txtPwd.attributedPlaceholder = [[NSAttributedString alloc] initWithString:@"密码" attributes:@{NSForegroundColorAttributeName:color}];
    _txtPwd.tag = 2;
    [_txtPwd setSecureTextEntry:YES];
    [_txtPwd setFont:XCFONT(15)];
    [_txtPwd setKeyboardType:UIKeyboardTypeASCIICapable];
    [self.view addSubview:_txtPwd];
    [_txtPwd mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(verView.mas_bottom).offset(30);
        make.leading.equalTo(loginLabel);
        make.trailing.equalTo(self.view).offset(-20.0);
        make.height.mas_equalTo(40);
    }];
    
    UIView * lineView = [[UIView alloc] init];
    lineView.backgroundColor = color_f2f2f2;
    [self.view addSubview:lineView];
    [lineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.trailing.equalTo(_txtPwd);
        make.top.equalTo(_txtPwd.mas_bottom);
        make.height.mas_equalTo(1);
    }];
    
    _txtConfirmPwd = [[RegisterTextField alloc] initWithFrame:CGRectZero];
    _txtConfirmPwd.delegate = self;
    [_txtConfirmPwd addTarget:self action:@selector(textFieldDidChange:) forControlEvents:UIControlEventEditingChanged];
    [_txtConfirmPwd setBorderStyle:UITextBorderStyleNone];
    [_txtConfirmPwd setReturnKeyType:UIReturnKeyDone];
    _txtConfirmPwd.clearButtonMode = UITextFieldViewModeWhileEditing;
    _txtConfirmPwd.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
    _txtConfirmPwd.leftViewMode = UITextFieldViewModeAlways;
    [_txtConfirmPwd setBackgroundColor:[UIColor clearColor]];
    [_txtConfirmPwd setTextColor:UIColorFromRGB(0x343434)];
    _txtConfirmPwd.isShowTextBool = YES;
    _txtConfirmPwd.attributedPlaceholder = [[NSAttributedString alloc] initWithString:@"确认密码" attributes:@{NSForegroundColorAttributeName:color}];
    _txtConfirmPwd.tag = 2;
    [_txtConfirmPwd setSecureTextEntry:YES];
    [_txtConfirmPwd setFont:XCFONT(15)];
    [_txtConfirmPwd setKeyboardType:UIKeyboardTypeASCIICapable];
    [self.view addSubview:_txtConfirmPwd];
    [_txtConfirmPwd mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(lineView.mas_bottom).offset(10);
        make.leading.trailing.equalTo(_txtPwd);
        make.height.mas_equalTo(40);
    }];
    
    lineView = [[UIView alloc] init];
    lineView.backgroundColor = color_f2f2f2;
    [self.view addSubview:lineView];
    [lineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.trailing.equalTo(_txtConfirmPwd);
        make.top.equalTo(_txtConfirmPwd.mas_bottom);
        make.height.mas_equalTo(1);
    }];
    
    // 验证码
    _btnCode = [UIButton initWithVerificationCodeFrame:Rect(20,lineView.y+20, 95, 36)];
    _btnCode.layer.borderColor = color_ffbd5b.CGColor;
    [_btnCode addTarget:self action:@selector(getAuthCode:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:_btnCode];
    [_btnCode mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(lineView.mas_bottom).offset(20);
        make.leading.equalTo(lineView);
    }];
    
    _checkAgree = [[QCheckBox alloc] initWithDelegate:self];
    [_checkAgree setTitle:@"我同意" forState:UIControlStateNormal];
    [_checkAgree setTitleColor:UIColorFromRGB(0x555555) forState:UIControlStateNormal];
    _checkAgree.titleLabel.font = XCFONT(14);
    [self.view addSubview:_checkAgree];
    [_checkAgree mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.equalTo(_btnCode);
        make.top.equalTo(_btnCode.mas_bottom).offset(30);
        make.width.mas_equalTo(60);
        make.height.mas_equalTo(30);
    }];
    
    UIButton * agreementBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    agreementBtn.backgroundColor = [UIColor clearColor];
    [agreementBtn setTitle:NSLocalizedString(@"使用协议", nil) forState:UIControlStateNormal];
    [agreementBtn setTitleColor:color_d2d2d2];
    agreementBtn.titleLabel.font = [UIFont systemFontOfSize:15];
    [agreementBtn addTarget:self action:@selector(showDetailAgreementAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:agreementBtn];
    [agreementBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.equalTo(_checkAgree.mas_trailing).offset(5);
        make.centerY.equalTo(_checkAgree);
    }];
    
    _signUpBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    _signUpBtn.backgroundColor = color_d2d2d2;
    [_signUpBtn setTitle:NSLocalizedString(@"注册", nil) forState:UIControlStateNormal];
    [_signUpBtn setTitleColor:COLOR_Bg_White];
    _signUpBtn.titleLabel.font = [UIFont systemFontOfSize:15];
    [_signUpBtn addTarget:self action:@selector(signUpAction:) forControlEvents:UIControlEventTouchUpInside];
    _signUpBtn.enabled = NO;
    [self.view addSubview:_signUpBtn];
    [_signUpBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.trailing.equalTo(verView);
        make.top.equalTo(_checkAgree.mas_bottom).offset(30);
        make.height.mas_equalTo(44);
    }];
}

#pragma mark - event response
- (void)loginAction:(UIButton *)sender {
    [self.navigationController popToRootViewControllerAnimated:YES];
}

- (void)signUpAction:(UIButton *)sender {
    Loading_Pig_Show(self.view);
    
    if (![[self.txtPwd.text trim] isEqualToString:[self.txtConfirmPwd.text trim]]) {
        [PopupViewHUD showSuccessPopup:@"密码不一致,请重新输入"];
        return;
    }
    
    NSDictionary * pDict = @{@"password":[self.txtPwd.text trim],
                             @"regName":self.phoneNum,
                             @"regType":@(self.regType),
                             @"ecode":self.validateCode,
                             @"phoneCode":self.validateCode,
                             };
    [[UserService sharedUserService] registerAccount:pDict success:^(id responseObject) {
        Loading_Hide(self.view);
        if ([responseObject isKindOfClass:[NSDictionary class]]) {
            BaseModel * baseModel = [[BaseModel alloc] initWithDictionary:responseObject error:nil];
            
            if (SuccessCode == baseModel.code) {
                [PopupViewHUD showSuccessPopup:baseModel.msg];
                [self.navigationController popToRootViewControllerAnimated:YES];
            } else {
                [PopupViewHUD showErrorPopup:baseModel.msg];
            }
        }
    } fail:^(NSError *error) {
        Loading_Hide(self.view);
    }];
}

- (void)showDetailAgreementAction:(UIButton *)sender {
    
}

- (void)getAuthCode:(UIButton *)sender {
    if (self.phoneNum.length == 0) {
        [PopupViewHUD showErrorPopup:@"手机号码"];
        return;
    }
    Loading_Pig_Show(self.view);
    [[UserService sharedUserService] getValidateCode:@{@"phone":self.phoneNum,@"type":@(12)} success:^(id responseObject) {
        Loading_Hide(self.view);
        if ([responseObject isKindOfClass:[NSDictionary class]]) {
            BaseModel * baseModel = [[BaseModel alloc] initWithDictionary:responseObject error:nil];
            
            if (SuccessCode == baseModel.code) {
                [PopupViewHUD showSuccessPopup:baseModel.msg];
            } else {
                [PopupViewHUD showErrorPopup:baseModel.msg];
            }
        }
    } fail:^(NSError *error) {
        Loading_Hide(self.view);
    }];
}

#pragma mark - UITextFieldDelegate
- (void)textFieldDidChange:(UITextField *)sender
{
    [self checkLogBtnIsEnable];
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    [self checkLogBtnIsEnable];
    
    return YES;
}

#pragma mark - QCheckBoxDelegate
- (void)didSelectedCheckBox:(QCheckBox *)checkbox checked:(BOOL)checked {
    checkbox.checked = checked;
    [self checkLogBtnIsEnable];
}

#pragma mark - private methods
- (void)checkLogBtnIsEnable {
    if (6 == self.validateCode.length && [self.txtPwd.text trim].length > 0 && [self.txtConfirmPwd.text trim].length > 0 && self.checkAgree.checked) {
        self.signUpBtn.enabled = YES;
    } else {
        self.signUpBtn.enabled = NO;
    }
}

- (void)startTimer
{
    nSecond = 59;
    if (_timer)
    {
        _timer = nil;
    }
    _timer = [NSTimer scheduledTimerWithTimeInterval:1.0f target:self selector:@selector(animation1) userInfo:nil repeats:YES];
}

- (void)animation1
{
    if (nSecond==0)
    {
        _btnCode.enabled  = YES;
        [_btnCode setTitle:@"获取验证码" forState:UIControlStateNormal];
        [_timer invalidate];
        return ;
    }
    NSString *strInfo = [NSString stringWithFormat:@"重新发送(%d)",nSecond];
    [_btnCode setTitle:strInfo forState:UIControlStateNormal];
    nSecond--;
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
